package com.project.sianet.data;

import java.util.List;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.Html;

import com.project.sianet.R;
import com.project.sianet.entidades.Libreta;
import com.project.sianet.fragment.*;

public class LibretaFragmentPagerAdapter extends FragmentPagerAdapter {

    private String[] mTabs;
    private String mLogin;
    private List<Libreta> mLibreta;
    private Context mContext;
    private int mSubTitleFrag;
	private String mTitleFrag;
    
    public LibretaFragmentPagerAdapter(FragmentManager fm, Context context, String login, List<Libreta> mLibreta, String titleFrag, int subTitleFrag) {
        super(fm);
        this.mLogin = login;
        this.mLibreta = mLibreta;
        this.mContext = context;
        this.mTitleFrag = titleFrag;
        this.mSubTitleFrag = subTitleFrag;
        mTabs = getPeriodo();
    }

    @Override
    public Fragment getItem(int index) {

        return LibretaDetalleFragment.newInstance(mLogin, mLibreta, index + 1, mTitleFrag, mSubTitleFrag, mTabs[index], getCount() - 1);
        
    }
    
    private String[] getPeriodo() {
    	String[] periodo;
    	String abrvBim;
    	
    	if (mLibreta.size() > 0) {
    		
    		// Si el codigo del curso es -1 significa que el alumno/familia
    		// se encuentra con la libreta virtual bloqueada
    		if (mLibreta.size() == 1 && mLibreta.get(0).getCodigoCurso() == -1) {
    			
    			periodo = new String[] { Html.fromHtml(mLibreta.get(0).getCodigoAlumno()).toString() };
    			
    		} else {
    		
	    		switch (mLibreta.get(0).getCantidadBimestres()) {
	    		
	    			case 1:
	    				abrvBim = mContext.getString(R.string.lblAbrvAnual);
	    				periodo = new String[] { abrvBim, mContext.getString(R.string.lblAbrvFinal) };
	    				break;
	    			case 2:
	    				abrvBim = mContext.getString(R.string.lblAbrvSem);
	    				periodo = new String[] { abrvBim + " 1", abrvBim + " 2", mContext.getString(R.string.lblAbrvFinal) };
	    				break;
	    			case 3:
	    				abrvBim = mContext.getString(R.string.lblAbrvTrim);
	    				periodo = new String[] { abrvBim + " 1", abrvBim + " 2", abrvBim + " 3", mContext.getString(R.string.lblAbrvFinal) };
	    				break;
	    			case 4:
	    			default:
	    				abrvBim = mContext.getString(R.string.lblAbrvBim);
	    	    		periodo = new String[] { abrvBim + " 1", abrvBim + " 2", abrvBim + " 3", abrvBim + " 4", mContext.getString(R.string.lblAbrvFinal) };
	    				break;
	    		
	    		}
	    		
    		}
    		
    	} else {
//    		abrvBim = mContext.getString(R.string.lblAbrvBim);
//    		periodo = new String[] { abrvBim + " 1", abrvBim + " 2", abrvBim + " 3", abrvBim + " 4", mContext.getString(R.string.lblAbrvFinal) };
    		periodo = new String[] { "No existen notas registradas" };
    	}
    	
    	return periodo;
    }

    @Override
    public int getCount() {
        return mTabs.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTabs[position];
    }
    
}
